import BaseComponent from "./BaseComponent";
import {Button as AntButton, Checkbox, Input, Modal, Radio, Select} from "antd";
import React from "react";
import SysDataPresenter from "../../.gen/presenter/SysDataPresenter";

export default class RSelect extends BaseComponent {

    static defaultProps = {
        dictCode: undefined,
    };

    constructor(props) {
        super(props);
        this.childRef = React.createRef();
        this.state = {
            data: [],
            visible:false
        }
        this.presenter = new SysDataPresenter(this);
        let dictCode = this.props.dictCode;
        if (dictCode && !this.props.children) {
            this.loadDictOptions(dictCode);
        }
    }

    // 根据字典code查询字典项
    loadDictOptions(dictCode) {
        this.presenter.findDictionaryList({dictCode: dictCode, model: this.props.model}, undefined, (res) => {
            if (res.code === 0) {
                let dictOptions = res.data.map(item => ({
                    value: this.props.dictValue ? item['' + this.props.dictValue] : item.dicValue,
                    label: this.props.dictText ? item['' + this.props.dictText] : item.dicName
                }))
                this.setState({dictOptions: dictOptions});
            } else {
                console.error('getDictItems error: : ', res)
            }
        })
    }

    render() {
        let {data} = this.state;
        if (!data){
            data = [];
        }
        let value = data.join(',');
        let props = {...this.props};
        let {bsView} = this.props
        delete props.dictCode;
        delete props.type;
        if (!bsView.ref){
            bsView.ref = this.childRef;
        }
        return (
            <>
                <Input
                    value={value}
                    allowClear={true}
                    editable={false} onClick={()=>{
                        if (bsView){
                            this.setState({
                                visible:true
                            })
                        }
                    }
                }/>
                <Modal
                    closable={true}
                    visible={this.state.visible}
                    onOk={()=>{
                        console.log(this.childRef)
                        console.log(this.childRef.getSelectData)
                        if (this.childRef&&this.childRef.getSelectData){
                            let data = bsView.getSelectData()
                            if (!data || data.length <= 1){
                                this.toastFail('请选择数据');
                                return;
                            }
                            this.setState({
                                data:data,
                                visible:true
                            })
                        }
                    }}
                    onCancel={()=>{
                        this.setState({
                            visible:false
                        })
                    }}
                >
                    {bsView}
                </Modal>
            </>
        )
    }
}